// pages/team/start-team.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 加载图标的大小
    sysHeight: 0,
    // 控制加载
    loading: 0,
    // banner数据
    banner: [],
    // 红包推荐弹出框
    activeDialogShow: false,
    // 是否为被邀请
    isInvite: 0,
    // 控制轮播图自动播放
    autoplay: true,
  },
  onLoad: function (options) {
    var scene = decodeURIComponent(options.scene);
    var newUid = options.uid;
    this.loginWay(scene, newUid);
  },
  onShow: function () {
    var that = this;
    // 获取本地userInfo
    let userInfo = wx.getStorageSync('userInfo');
    // 如果本地缓存中存在userInfo 赋值给改界面的userInfo
    if (userInfo !== '') {
      that.setData({
        userInfo: userInfo,
      });
      if (that.data.isInvite) that.isUidChange();
      if (that.data.activeDialogShow) {
        wx.showTabBar();
        that.setData({
          activeDialogShow: false
        });
      }
    } else {
      // 用户未登录弹红包
      that.openActiveDialog();
    }
    // 获取设备信息
    let info = wx.getSystemInfoSync();
    let sysHeight = info.windowHeight - 100;
    this.setData({
      sysHeight: sysHeight,
      autoplay: true,
    });
    this.getIndexData();
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.setData({
      loading: 0,
    })
    this.getIndexData();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
  },
  onShareAppMessage: function () {},
  // 监听界面隐藏后取消轮播图的自动滚动功能
  onHide: function () {
    this.setData({
      autoplay: false
    })
  },
  goActiveShare() {
    let res = util.loginNow();
    if (res == true) {
      wx.navigateTo({
        url: '/pages/active-share/index',
      })
    }
  },
  goActiveHelp() {
    let res = util.loginNow();
    if (res == true) {
      wx.navigateTo({
        url: '/pages/active-help/index',
      })
    }
  },
  // 获取首页所有数据（banner productList iconList）
  getIndexData: function () {
    let that = this;
    util.requestApi(api.getIndex, ).then(function (res) {
      if (res.code == 200) {
        that.setData({
          banner: res.content.indexBanner,
          loading: 1
        });
      }
      that.getCartNum();
    })
  },
  onClickHide() {
    wx.showTabBar();
    this.setData({
      activeDialogShow: false
    });
  },
  // 判断用户被邀请的方式
  loginWay(scene, newUid) {
    if (scene == "undefined" && newUid == undefined) {
      wx.setStorageSync('newUid', undefined);
    } else {
      if (scene == "undefined") {
        wx.setStorageSync('newUid', newUid);
      } else {
        var id = scene.slice(4)
        wx.setStorageSync('newUid', id);
      }
      this.setData({
        isInvite: 1
      })
    }
  },
  // 判断uid是否发生变化
  isUidChange() {
    let uid = wx.getStorageSync('uid'),newUid = wx.getStorageSync('newUid'),userID=wx.getStorageSync('userID');
    if (uid !== newUid&&newUid!=userID&& newUid!== undefined) {
      wx.setStorageSync('uid', newUid);
      // 给邀请人发送5元优惠券
      this.sendCoupons(newUid, 2)
    }
  },
  goActiveReceive() {
    let res = util.loginNow();
  },
  // 用户未登录弹红包
  openActiveDialog() {
    wx.hideTabBar();
    this.setData({
      activeDialogShow: true
    });
  },
  // 发送优惠券
  sendCoupons(uid, type) {
    util.requestApi(api.sendCoupons, {
      userID: uid,
      type: type
    }).then(function (res) {
      if (res.code === 200) {}
    });
  },
  // 跳转定制界面
  goDiyList() {
    wx.switchTab({
      url: '/pages/diy-list/index',
    });
  },
  // 跳转企业定制
  goEntDiy(){
    wx.navigateTo({
      url: '/pages/ent-diy/index',
    });
  },
  // 获取购物车数量
  getCartNum: function () {
      let userID = wx.getStorageSync('userID')
      if (userID !== '' && userID !== undefined) {
          util.requestApi(api.getUserCartCount, {
              userID: userID
          }).then(function (res) {
              if (res.code === 200) {
                  let cartGoodsCount = '';
                  cartGoodsCount = res.content + '';
                  wx.setTabBarBadge({
                      index: 3,
                      text: cartGoodsCount
                  })
              } else {
                  wx.removeTabBarBadge({
                      index: 3,
                  })
              }
          });
      }
  },
})